package com.first.system.service.Impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.AclUser;
import com.first.system.mapper.UserMapper;
import com.first.system.service.UserService;
import com.first.system.vo.AddUserVo;
import com.first.system.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @program: finrsteducation-parent
 * @description:
 * @author: 张玉森
 * @create: 2023-05-22 20:44
 **/
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    /*
     *条件查询用户表
     * 页码：每页个数：条件
     * */
    @Override
    public Page<AclUser> findByPage(Integer currentPage, Integer pageSize, UserVo userVo) {
        Page<AclUser> page = new Page<>(currentPage, pageSize);
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted", 0);
        if (userVo.getUsername() != null) {
            wrapper.like("username", userVo.getUsername());
        }
        if (userVo.getStartDate() != null) {
            wrapper.gt("gmt_create", userVo.getStartDate());
        }
        if (userVo.getEndDate() != null) {
            wrapper.lt("gmt_create", userVo.getEndDate());
        }
        Page<AclUser> userPage = userMapper.selectPage(page, wrapper);
        return userPage;
    }

    @Override
    public Integer updateStatus(String id, Integer status) {
        AclUser user = new AclUser();
        user.setId(id);
        user.setStatus(status);
        user.setGmtModified(LocalDateTime.now());
        Integer update = userMapper.updateById(user);
        return update;
    }

    @Override
    public Integer addUser(AddUserVo addUserVo) {
        AclUser user = new AclUser();
        user.setUsername(addUserVo.getUsername());
        user.setNickName(addUserVo.getNickName());
        user.setHeadUrl(addUserVo.getUserUrl());
        user.setStatus(0);
        user.setIsDeleted(0);
        user.setPassword(DigestUtil.md5Hex("123456"));
        user.setGmtCreate(LocalDateTime.now());
        user.setGmtModified(LocalDateTime.now());
        int insert = userMapper.insert(user);
        return insert;
    }

    @Override
    public Integer updateUser(AclUser aclUser) {
        aclUser.setGmtModified(LocalDateTime.now());
        int i = userMapper.updateById(aclUser);
        return i;
    }

    @Override
    public Integer deletedById(String id) {
        AclUser user = new AclUser();
        user.setId(id);
        user.setIsDeleted(1);
        user.setGmtModified(LocalDateTime.now());
        return userMapper.updateById(user);
    }

    @Override
    public String selectPassword(String username, String password) {
        QueryWrapper<AclUser> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        wrapper.eq("password", DigestUtil.md5Hex(password));
        AclUser user = userMapper.selectOne(wrapper);
        if (user == null) {
            return "密码错误";
        }
        return "密码正确";
    }

    @Override
    public Integer updatePassword(AclUser aclUser, String password) {
        aclUser.setPassword(DigestUtil.md5Hex(password));
        aclUser.setGmtModified(LocalDateTime.now());
        return userMapper.updateById(aclUser);
    }
}

